package com.stottlerhenke.simbionic.editor;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import com.stottlerhenke.simbionic.api.SB_VarType;
import com.stottlerhenke.simbionic.editor.gui.ComponentRegistry;
import com.stottlerhenke.simbionic.editor.gui.SB_ProjectBar;

public class SB_Predicate extends SB_Function
{
    private static final long serialVersionUID = 2302585093L + 3;

    protected int _retType = SB_Variable.kBoolean;

    protected static ImageIcon _icon = null;
    protected static ImageIcon _coreIcon = null;

    transient private String _fullName = null;

    public SB_Predicate()  // constructor for Externalizable object
    {
    }

    public SB_Predicate(String name)
    {
        super(name);
    }

    public SB_Predicate(String name, int retType)
    {
      super(name);
      _retType = retType;
    }

    public String toString()
    {
      if (_editing)
        return _name;
      else
      {
        if (_fullName == null)
            _fullName = _name + " : " + getRetTypeName();
        return _fullName;
      }
    }

    public void setName(String name) { super.setName(name); _fullName = null; }
    public int getRetType() { return _retType; }
    public void setRetType(int retType) { 
    	_retType = retType; 
    	_fullName = null; 
    }
    
    public String getRetTypeName(){
    	SB_TypeManager typeManager = ComponentRegistry.getProjectBar().getTypeManager();
    	SB_VarType newType = SB_TypeManager.convertOldTypeId(_retType);
    	return typeManager.getTypeName(newType);
    }
    
    public Icon getIcon()
    {
      if (_core)
      {
        if (_coreIcon == null)
          _coreIcon = Util.getImageIcon("CorePredicate.gif");
        return _coreIcon;
      }
      else
      {
        if (_icon == null)
          _icon = Util.getImageIcon("Predicate.gif");
        return _icon;
      }
    }

    public void writeExternal(ObjectOutput out) throws IOException
    {
      super.writeExternal(out);
      out.writeInt(_retType);
    }

    public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException
    {
      super.readExternal(in);
      _retType = in.readInt();

      int loading = SB_ProjectBar.getLoading();
      if ((loading == SB_ProjectBar.LOADING_SBP && (SB_ProjectBar.getSBPVersion() == 5 || SB_ProjectBar.getSBPVersion() == 6))
              || (loading == SB_ProjectBar.LOADING_APD && SB_ProjectBar.getAPDVersion() == 3)
              || (loading == SB_ProjectBar.LOADING_BTN && SB_ProjectBar.getBTNVersion() == 5))
      {
      	String retTypeName = (String) in.readObject();
      }
    }

    public String getTag() { return "predicate"; }

    public void exportXML(BufferedWriter out, int n) throws IOException
    {
      super.exportXML(out, n);
      SB_ProjectBar.writeTabs(out, n);
      out.write("<ret>" + _retType + "</ret>");
      out.newLine();
    }
}
